package com.scale;

import com.ruoyi.RuoYiApplication;
import com.ruoyi.scale.document.Scale;
import com.ruoyi.scale.model.GrowthNormData;
import com.ruoyi.scale.model.GrowthNormDataWrap;
import com.ruoyi.scale.service.ScaleService;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.domain.Example;
import org.springframework.test.context.junit4.SpringRunner;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RunWith(SpringRunner.class)
@SpringBootTest(classes = RuoYiApplication.class)
public class GrowImport2 {
    @Resource
    private ScaleService scaleService;
    @Before
    public void before(){
        Scale queryWhere = new Scale();
        queryWhere.setRuleName("growthEvalRule");

        List<Scale> scales = scaleService.findAll(Example.of(queryWhere));
        for (Scale scale : scales) {
            scaleService.delete(scale);
        }

    }

    public static void main(String[] args) throws IOException {
        new GrowImport2().start();
    }

    @Test
    public void start() throws IOException {
        Map<String, List<List<Object>>> resolver = com.scale.Test.resolver();
        Scale scale = new Scale();
        scale.setName("儿童体格生长测评");
        scale.setRuleName("growthEvalRule");
        scale.setDelFlag(0);
        scale.setCode("growth");
        scale.setInputFormClass("com.ruoyi.scale.model.GrowthInput");
        GrowthNormDataWrap wrap = new GrowthNormDataWrap();

        Map<String,List<GrowthNormData>> normData = new HashMap<>();
        wrap.setNormData(normData);

        for (Map.Entry<String, List<List<Object>>> entry : resolver.entrySet()) {
            String key = entry.getKey();
            String val = key.split(" ")[0];
            List<GrowthNormData> list = new ArrayList<>();

            for (int i = 1; i < entry.getValue().size(); i++) {
                List<Object> objects = entry.getValue().get(i);

                GrowthNormData data = new GrowthNormData();
                data.setKeyVal((Double)objects.get(0));
                data.setP3((Double)objects.get(1));
                data.setP10((Double)objects.get(2));
                data.setP25((Double)objects.get(3));
                data.setP50((Double)objects.get(4));
                data.setP75((Double)objects.get(5));
                data.setP90((Double)objects.get(6));
                data.setP97((Double)objects.get(7));
                list.add(data);
            }
            normData.put(val,list);

        }
        scale.setNormData(wrap);
        scaleService.save(scale);
    }
}
